//
//  StatusCell.swift
//  Weibo
//
//  Created by 周先生 on 15/11/15.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellIconWidth: CGFloat = 35
class StatusCell: UITableViewCell {

    
    //原创微博底部约束 用来记录底部约束 后续根据是否有配图 来动态修改
    var bottomConstraints: Constraint?

    //数据模型
    var status: Status? {
        didSet {
        
            originalView.status = status
            //移除约束
            self.bottomConstraints?.uninstall()
            //设置转发微博的数据模型
            if let re = status?.retweeted_status {
                //设置数据源
                reweetedView.reweetedStatus = re
                //设置显示
                reweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraints = make.top.equalTo(reweetedView.snp_bottom).constraint
                })
            } else {
                //设置隐藏
                reweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
            
            }
        }
        
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        //调用setupUI
        setupUI()
       }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")

    }
    //设置UI
    private func setupUI() {
    
        //添加子视图
        contentView.addSubview(originalView)
        
        contentView.addSubview(reweetedView)
        
        contentView.addSubview(bottomView)
        //设置约束
        //原创
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(50)
        }
        //转发微博
        reweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        //底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
         self.bottomConstraints = make.top.equalTo(reweetedView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
        /**
        *  容器视图的底部约束 是自动计算行高的关键
        */
        contentView.snp_makeConstraints { (make) -> Void in
            //contentView底部约束是最关键的一点
            make.bottom.equalTo(bottomView.snp_bottom)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
        }
    }

    //懒加载子控件
    private lazy var originalView : StatusOriginalView = StatusOriginalView()
    private lazy var bottomView : StatusBottomView = StatusBottomView()
    
    private lazy var reweetedView: ReweetedStatusView = ReweetedStatusView()
    
}
